package jp.co.ricoh.melody.views.adapters;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import jp.co.ricoh.melody.R;
import jp.co.ricoh.melody.models.entities.SoundData;
import jp.co.ricoh.melody.presenters.ISoundRecyclerViewPresenter;
import jp.co.ricoh.melody.presenters.SDCardSoundRecyclerViewPresenterImpl;

/**
 * Created by tanyajie on 2016/11/17.
 */

public class SDCardSoundRecycleView extends RecyclerView implements ISDCardSoundRecycleView {
    private Context mContext;
    /**
     * Presenter of view.
     */
    private ISoundRecyclerViewPresenter mISoundListPresenter;

    public SDCardSoundRecycleView(Context context) {
        super(context);
        init();
    }

    public SDCardSoundRecycleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SDCardSoundRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    /**
     * Initialize this view.
     *
     * @param context
     */
    @Override
    public void onCreate(Context context, SoundData soundData) {
        init();
        mContext = context;
        this.addItemDecoration(new DividerItemDecoration(mContext));
        mISoundListPresenter = new SDCardSoundRecyclerViewPresenterImpl();
        mISoundListPresenter.onCreate(mContext, this, soundData);
    }

    private void init() {
        setLayoutManager(new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                int visibleCount = findLastCompletelyVisibleItemPosition() - findFirstCompletelyVisibleItemPosition();
                int itemCount = mISoundListPresenter.getItemCount();
                return itemCount > visibleCount && super.canScrollVertically();
            }
        });
    }

    /**
     * This method is used to get current selected item on recycler view.
     *
     * @return soundData
     */
    @Override
    public SoundData getSelectedItem() {
        return mISoundListPresenter.getCurrentSelectedItem();
    }

    @Override
    public void onDestroyView() {
        mISoundListPresenter.onDestroyView();
    }

    @Override
    public SoundItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.sound_item_list, parent, false);
        return new SoundItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SoundItemViewHolder viewHolder, SoundData itemData) {

    }


    /**
     * View holder of this recycle view.
     */
    public class SoundItemViewHolder extends ViewHolder {
        /**
         * This view is used to display name of sound.
         */
        public Button mItemBtn;

        public SoundItemViewHolder(View itemView) {
            super(itemView);
            mItemBtn = (Button) itemView.findViewById(R.id.sound_item_list_txt);
        }
    }

    /**
     * This class is used to draw divider for item of recycle view.
     */
    private class DividerItemDecoration extends ItemDecoration {
        private final int[] ATTRS = new int[]{
                android.R.attr.listDivider
        };

        private Drawable mDivider;

        public DividerItemDecoration(Context context) {
            final TypedArray a = context.obtainStyledAttributes(ATTRS);
            mDivider = a.getDrawable(0);
            a.recycle();
        }

        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, State state) {
            // item padding value.
            int itemMarginHorizontal = (int) mContext.getResources().getDimension(R.dimen.sound_item_horizontal_margin);
            // left padding value of divider.
            int left = parent.getPaddingLeft() + itemMarginHorizontal;
            // right padding value of divider.
            int right = parent.getWidth() - parent.getPaddingRight() - itemMarginHorizontal;

            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                LayoutParams params = (LayoutParams) child
                        .getLayoutParams();
                int top = child.getBottom() + params.bottomMargin;
                int bottom = top + mDivider.getIntrinsicHeight();
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        }
    }


}